/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelPaciente.java
 *
 * Created on 27/11/2011, 17:08:25
 */

package oogenda.view;

import java.util.AbstractList;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import oogenda.entidade.Paciente;
import oogenda.model.PacienteModelo;
import oogenda.util.DadosObjetos;

/**
 *
 * @author Pablo
 */
public class PanelPaciente extends javax.swing.JPanel {

    private static PanelPaciente panelPaciente;
    private static int incrementaID = 1;

    public static PanelPaciente getPanel() {
        if(panelPaciente == null) {
            panelPaciente = new PanelPaciente();
        }
        panelPaciente.popularTabela();
        return panelPaciente;
    }

    public void popularTabela() {
        PacienteModelo modelo = new PacienteModelo();
        tabelaPacientes.setModel(modelo);
        if (DadosObjetos.pacientes != null) {
            for (Paciente paciente : DadosObjetos.pacientes) {
                modelo.addRow(paciente);
            }
        }
    }

    /** Creates new form PanelPaciente */
    private PanelPaciente() {
        initComponents();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        titulo = new javax.swing.JLabel();
        nome = new javax.swing.JLabel();
        lbRg = new javax.swing.JLabel();
        campoRG = new javax.swing.JFormattedTextField();
        campoNome = new javax.swing.JTextField();
        campoEndereco = new javax.swing.JTextField();
        lbTelefone = new javax.swing.JLabel();
        campoTelefone = new javax.swing.JFormattedTextField();
        lbEndereco = new javax.swing.JLabel();
        CPF = new javax.swing.JLabel();
        campoCPF = new javax.swing.JFormattedTextField();
        campoDataNascimento = new javax.swing.JFormattedTextField();
        dataNascimento = new javax.swing.JLabel();
        btSalvar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaPacientes = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        titulo.setFont(new java.awt.Font("Verdana", 1, 14));
        titulo.setText("Pacientes");

        nome.setText("Nome:");

        lbRg.setText("RG:");

        campoRG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoRGActionPerformed(evt);
            }
        });

        lbTelefone.setText("Telefone:");

        campoTelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoTelefoneActionPerformed(evt);
            }
        });

        lbEndereco.setText("Endereço:");

        CPF.setText("CPF:");

        campoCPF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoCPFActionPerformed(evt);
            }
        });

        campoDataNascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoDataNascimentoActionPerformed(evt);
            }
        });

        dataNascimento.setText("Data de nascimento:");

        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        tabelaPacientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tabelaPacientes);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(titulo, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                .addGap(115, 115, 115))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nome)
                    .addComponent(lbRg)
                    .addComponent(lbEndereco)
                    .addComponent(lbTelefone)
                    .addComponent(dataNascimento))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(campoDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(campoRG, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(CPF)
                        .addGap(18, 18, 18)
                        .addComponent(campoCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(73, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(398, Short.MAX_VALUE)
                .addComponent(btSalvar)
                .addGap(60, 60, 60))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(titulo)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nome)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbRg)
                    .addComponent(campoRG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CPF)
                    .addComponent(campoCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbEndereco)
                            .addComponent(campoEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbTelefone)
                            .addComponent(campoTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dataNascimento)
                            .addComponent(campoDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(59, 59, 59))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btSalvar)
                        .addGap(34, 34, 34)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void campoRGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoRGActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_campoRGActionPerformed

private void campoTelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoTelefoneActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_campoTelefoneActionPerformed

private void campoCPFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoCPFActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_campoCPFActionPerformed

private void campoDataNascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoDataNascimentoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_campoDataNascimentoActionPerformed

private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        salvar();
}//GEN-LAST:event_btSalvarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CPF;
    private javax.swing.JButton btSalvar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JFormattedTextField campoCPF;
    private javax.swing.JFormattedTextField campoDataNascimento;
    private javax.swing.JTextField campoEndereco;
    private javax.swing.JTextField campoNome;
    private javax.swing.JFormattedTextField campoRG;
    private javax.swing.JFormattedTextField campoTelefone;
    private javax.swing.JLabel dataNascimento;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lbEndereco;
    private javax.swing.JLabel lbRg;
    private javax.swing.JLabel lbTelefone;
    private javax.swing.JLabel nome;
    private javax.swing.JTable tabelaPacientes;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables

    private void salvar() {
       String nomePaciente = campoNome.getText();
       String cpf = campoCPF.getText();
       String rg = campoRG.getText();
       String endereco = campoEndereco.getText();
       String telefone = campoTelefone.getText();
       String dataNasc = campoDataNascimento.getText();
               
       if(nomePaciente == null || nomePaciente.isEmpty()) {
            JOptionPane.showMessageDialog(null, "O campo nome do paciente deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }
       
       if((cpf == null || cpf.isEmpty()) && (rg == null || rg.isEmpty())) {
            JOptionPane.showMessageDialog(null, "O campo cpf ou o campo rg deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }
       
       if(endereco == null || endereco.isEmpty()) {
            JOptionPane.showMessageDialog(null, "O campo endereço deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }
       
       if(telefone == null || telefone.isEmpty()) {
            JOptionPane.showMessageDialog(null, "O campo telefone deve ser preenchido.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }
       
       Paciente paciente = new Paciente();
       paciente.setNome(nomePaciente);
       paciente.setCpf(cpf);
       paciente.setIdentidade(rg);
       paciente.setEndereco(endereco);
       paciente.setTelefone(telefone);
       if(dataNasc != null && !dataNasc.isEmpty())
           paciente.setDataNascimento(dataNasc);
       paciente.setId(incrementaID);
       incrementaID++;
       if(DadosObjetos.pacientes == null) {
            DadosObjetos.pacientes= new ArrayList<Paciente>();
       }
       DadosObjetos.pacientes.add(paciente);
       
       JOptionPane.showMessageDialog(null, "Dados Salvos", "Sucesso", JOptionPane.INFORMATION_MESSAGE, null);
       campoNome.setText(null);
       campoCPF.setText(null);
       campoRG.setText(null);
       campoEndereco.setText(null);
       campoTelefone.setText(null);
       campoDataNascimento.setText(null);

       popularTabela();
    }
}
